package com.hh.service.Impl;

import com.hh.dto.ResultDTO;
import com.hh.mapper.UserMapper;
import com.hh.model.UserModel;
import com.hh.service.UserService;
import com.hh.until.JSONCopy;
import com.hh.until.Md5;
import com.hh.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
//    UserDAO userDAO = new UserDAOImpl();

    @Autowired
    private UserMapper userMapper;
    /**
     * 登录
     *
     * @param userVO
     * @return
     */
    @Override
    public ResultDTO login(UserVO userVO) {
        ResultDTO resultDTO = new ResultDTO();
        if (userVO.getCode().equals(userVO.getSessionCode())) {
            UserModel userModel = new UserModel();
//            判断用户名和密码是否正确
//            vo不能进入dao层：只能止步于servlet和service
//            dao层只能用model
//            所以需要将vo里面的数据赋值到model
            userModel = JSONCopy.copy(userVO, UserModel.class);
            userModel.setPassword(Md5.encoderByMd5(userModel.getPassword()));//密码加密
            UserModel model = userMapper.findByUsernameAndPassword(userModel);

            if (model != null) {
                resultDTO.setCode(1);
                resultDTO.setMsg("登录成功！");
            } else {
                resultDTO.setCode(0);
                resultDTO.setMsg("error");
            }
        } else {
            resultDTO.setCode(-1);
            resultDTO.setMsg("验证码错误！");
        }

        return resultDTO;
    }

    /**
     * 用户添加
     *
     * @param userVO
     * @return
     */
    @Override
    public ResultDTO add(UserVO userVO) {
        UserModel userModel = JSONCopy.copy(userVO, UserModel.class);
        userModel.setPassword(Md5.encoderByMd5(userModel.getPassword()));
//        int count = userDAO.add(userModel);
        int count = userMapper.add(userModel);
        ResultDTO resultDTO = ResultDTO.builder().count(count).code(1).msg("添加成功").build();
        return resultDTO;
    }


    /**
     * 查询所有用户信息
     *
     * @return
     */
    @Override
    public ResultDTO findAll(UserVO userVO) {
        UserModel userModel = JSONCopy.copy(userVO, UserModel.class);
//        List<UserModel> list = userDAO.findAll(userModel);
        userModel.setPage((userModel.getPage()-1)*userModel.getLimit());
        List<UserModel> list = userMapper.findAll(userModel);
        System.out.println(list);
        return ResultDTO.getDto(userMapper.count(userModel),list);
    }


    /**
     * 通过id查询数据
     *
     * @param userVO
     * @return
     */
    @Override
    public ResultDTO findById(UserVO userVO) {
        ResultDTO resultDTO = ResultDTO.builder().build();
        UserModel userModel = JSONCopy.copy(userVO, UserModel.class);
//        UserModel model = userDAO.findById(userModel);
        UserModel model = userMapper.findById(userModel);
        if (model == null) {
            resultDTO.setCode(0);
            resultDTO.setMsg("没有查到数据！");
        } else {
            resultDTO.setCode(1);
            resultDTO.setData(model);
        }
        return resultDTO;
    }

    @Override
    public ResultDTO count(UserVO userVO) {
        UserModel userModel = JSONCopy.copy(userVO, UserModel.class);
//        return ResultDTO.getDto(userDAO.count(userModel),null);
        return ResultDTO.getDto(userMapper.count(userModel),null);
    }

    @Override
    public ResultDTO delete(UserVO userVO) {
        ResultDTO resultDTO = ResultDTO.builder().build();
        UserModel userModel = JSONCopy.copy(userVO, UserModel.class);
//        int count = userDAO.delete(userModel);
        int count = userMapper.delete(userModel);
        if (count > 0) {
            resultDTO.setCode(1);
            resultDTO.setMsg("删除成功！");
        } else {
            resultDTO.setCode(0);
            resultDTO.setMsg("删除失败！");
        }
        return resultDTO;
    }

    @Override
    public ResultDTO updatePassword(UserVO userVO) {
        ResultDTO resultDTO = ResultDTO.builder().build();
        UserModel userModel = JSONCopy.copy(userVO, UserModel.class);
        userModel.setPassword(Md5.encoderByMd5(userVO.getOldPassword()));
//        UserModel model = userDAO.findIdAndPassword(userModel);
        UserModel model = userMapper.findIdAndPassword(userModel);
        if (model == null) {
            resultDTO.setCode(-1);
            resultDTO.setMsg("原密码不正确，请重新输入！");
        } else {
            userModel.setPassword(Md5.encoderByMd5(userVO.getNewPassword()));
//            int count = userDAO.updatePassword(userModel);
            int count = userMapper.updatePassword(userModel);
            if (count > 0) {
                resultDTO.setCode(1);
                resultDTO.setMsg("修改成功！");
            }else {
                resultDTO.setCode(0);
                resultDTO.setMsg("修改失败！");
            }
        }
        return resultDTO;
    }

    @Override
    public ResultDTO update(UserVO userVO) {
        UserModel userModel = JSONCopy.copy(userVO, UserModel.class);
//        int count = userDAO.update(userModel);
        int count = userMapper.update(userModel);
        ResultDTO resultDTO = ResultDTO.builder().count(count).build();
        return resultDTO;
    }
}
